package com.qf.file.utils;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 30909
 * 解决群头像的合成问题
 */
public class GroupIconUtil {
    /**
     * f返回每一个小头像上角的顶点坐标
     */
    private static String[] getXY(int size) {
        if (size == 1) {
            return new String[]{"6,6"};
        }
        if (size == 2) {
            return new String[]{"4,36", "68,36"};
        }
        if (size == 3) {
            return new String[]{"36,4", "4,68", "68,68"};
        }
        if (size == 4) {
            return new String[]{"4,4", "68,4", "4,68", "68,68"};
        }
        if (size == 5) {
            return new String[]{"24,24",
                    "68,24",
                    "3,68",
                    "46,68",
                    "89,68"
            };
        }
        if (size == 6) {
            return new String[]{"3,24",
                    "46,24",
                    "89,24",
                    "3,68",
                    "46,68",
                    "89,68",
            };
        }
        if (size == 7) {
            return new String[]{
                    "46,3",
                    "3,46",
                    "46,46",
                    "89,46",
                    "3,89",
                    "46,89",
                    "89,89"
            };
        }
        if (size == 8) {
            return new String[]{
                    "24,3",
                    "68,3",
                    "3,46",
                    "46,46",
                    "89,46",
                    "3,89",
                    "46,89",
                    "89,89"
            };
        }
        if (size == 9) {
            return new String[]{
                    "3,3",
                    "46,3",
                    "89,3",
                    "3,46",
                    "46,46",
                    "89,46",
                    "3,89",
                    "46,89",
                    "89,89"
            };
        }
        return null;
    }


    //TODO 注意坐标
    /**
     * 获取头像的边长
     */
    private static int getWidth(int size) {
        if (size == 1) {
            return 120;
        }
        if (size >= 2 && size <= 4) {
            return 60;
        }
        if (size >= 5 && size <= 9) {
            return 40;
        }
        return 0;
    }

    /**
     * 0次IO 放缩图片的工具类
     *
     * @param sourceImage 原图片
     * @param width       目标宽度
     * @param height      目标高
     * @return 压缩之后的图片
     */
    public static BufferedImage zoom(BufferedImage sourceImage, int width, int height) {
        //放缩
        Image image = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);

        //做出一个空白图片来
        BufferedImage zoomImage = new BufferedImage(width, height, sourceImage.getType());

        //把 目标图片设置为空白
        Graphics graphics = zoomImage.getGraphics();
        //设置颜色
        //Color类使用一个 int 值来表达一个颜色
        // 由高八位，次高八位，低八位，次低八位 顺次表达  透明度、红、绿、蓝
        graphics.setColor(new Color(0xffffff));
        //绘制图片信息
        graphics.drawImage(image, 0, 0, null);

        //返回一张白色的图片
        return zoomImage;
    }

    /**
     * 创建群头像
     */
    public static BufferedImage createImage(List<BufferedImage> images) {
        //获取坐标位置
        String[] imagePosition = getXY(images.size());
        //获取小头像的边长
        int width = getWidth(images.size());

        //准备一张空白的 132 *132 的图片
        BufferedImage imageNew = new BufferedImage(132, 132, BufferedImage.TYPE_INT_RGB);

        //设置背景色为白色
        imageNew.getGraphics().setColor(Color.WHITE);

        //填充一个矩形
        imageNew.getGraphics().fillRect(0, 0, 132, 132);

        //添加描边
//        imageNew.getGraphics().drawRect();
//        Graphics graphics = imageNew.getGraphics();


        //把N个头像合成为一个群头像
        for (int i = 0; i < imagePosition.length; i++) {
            String position = imagePosition[i];
            //逗号分割为数组
            String[] positionArr = position.split(",");

            //提取
            int x = Integer.valueOf(positionArr[0]);
            int y = Integer.valueOf(positionArr[1]);
            //把第i个图片放缩指定比例
            BufferedImage imageOne = zoom(images.get(i), width, width);
            //把第i个图片绘制到132*12的那个图片上
            imageNew.getGraphics().drawImage(imageOne, x, y, null);
        }
        return imageNew;
    }

    public static void main(String[] args) throws IOException {
        String path = "C:\\test\\QFCodeUtil";
        List<BufferedImage> images = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            images.clear();
            for (int j = 1; j <= i; j++) {
                //  File.separator 斜杠分隔符
                BufferedImage image = ImageIO.read(new File(path + File.separator + j + ".jpg"));
                images.add(image);
            }
            //使用上面的方法合成图像
            BufferedImage group = createImage(images);
            ImageIO.write(group, "jpg", new File(path + File.separator + "group" + i + ".jpg"));
        }
    }
}
